package com.runen.infinitemovement.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.CompanyHomeRankingAdapter;
import com.runen.infinitemovement.model.api.CompanyAPI;
import com.runen.infinitemovement.model.entity.CompanyHomeTimesRankingEntity;
import com.runen.infinitemovement.model.entity.CompanyPersonRankEntity;
import com.runen.infinitemovement.model.entity.EventBusEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CompanyRankingFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
    @BindView(R.id.rv_company_ranking_list)
    RecyclerView rvCompanyRankingList;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    @BindView(R.id.tv_ranking_num)
    TextView tvRankingNum;
    @BindView(R.id.rci_user_header)
    RoundedCornersImage rciUserHeader;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_minute)
    TextView tvMinute;
    @BindView(R.id.tv_sports_num)
    TextView tvSportsNum;
    @BindView(R.id.tv_enter_num)
    TextView tvEnterNum;
    @BindView(R.id.ll_bg)
    LinearLayout llBg;
    @BindView(R.id.iv_ranking)
    ImageView ivRanking;
    @BindView(R.id.tv_img_ranking_num)
    TextView tvImgRankingNum;
    private View view;
    private int point;
    private int page = 2;
    private String companyId;

    public CompanyRankingFragment(int point, String companyId) {
        this.point = point;
        this.companyId = companyId;
    }

    private CompanyHomeRankingAdapter companyHomeRankingAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_company_ranking, container, false);
            ButterKnife.bind(this, view);
            initView();
            initData();
            initPerson();
        }
        return view;
    }

    private void initPerson() {
        String string = "?companyId=%s&type=%s";
        new CompanyAPI().getCompanyPersonRank(String.format(string, companyId, point), new BaseResultCallback<CompanyPersonRankEntity>() {
            @Override
            public void onResponse(CompanyPersonRankEntity response) {
                if (response.code == 0) {
                    if (response.data != null) {
                        switch (response.data.rank) {
                            case 1:
                                GlideUtils.loadImage(getActivity(), CommonUtil.getDrawable(R.mipmap.ico_ranking1), ivRanking);
                                tvImgRankingNum.setText(String.valueOf(response.data.rank));
                                ivRanking.setVisibility(View.VISIBLE);
                                tvImgRankingNum.setVisibility(View.VISIBLE);
                                tvRankingNum.setVisibility(View.GONE);
                                break;
                            case 2:
                                GlideUtils.loadImage(getActivity(), CommonUtil.getDrawable(R.mipmap.ico_ranking2), ivRanking);
                                tvImgRankingNum.setText(String.valueOf(response.data.rank));
                                ivRanking.setVisibility(View.VISIBLE);
                                tvImgRankingNum.setVisibility(View.VISIBLE);
                                tvRankingNum.setVisibility(View.GONE);
                                break;
                            case 3:
                                GlideUtils.loadImage(getActivity(), CommonUtil.getDrawable(R.mipmap.ico_ranking3), ivRanking);
                                tvImgRankingNum.setText(String.valueOf(response.data.rank));
                                ivRanking.setVisibility(View.VISIBLE);
                                tvImgRankingNum.setVisibility(View.VISIBLE);
                                tvRankingNum.setVisibility(View.GONE);
                                break;
                            default:
                                tvRankingNum.setVisibility(View.VISIBLE);
                                tvRankingNum.setText(String.valueOf(response.data.rank));
                                ivRanking.setVisibility(View.GONE);
                                tvImgRankingNum.setVisibility(View.GONE);
                                break;
                        }


                        tvUserName.setText(response.data.nickname);
                        GlideUtils.loadImage(getActivity(), response.data.headImgUrl, rciUserHeader);
                        tvMinute.setText(String.valueOf(response.data.sportTimeLength));
                        tvSportsNum.setText(String.valueOf(response.data.sportTimes));
                        tvEnterNum.setText(String.valueOf(response.data.activityTimes));


                    }
                }
            }
        });
    }

    private void initView() {
        /**
         * 排名信息
         */
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getActivity());
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        rvCompanyRankingList.setLayoutManager(layoutManager1);
        companyHomeRankingAdapter = new CompanyHomeRankingAdapter(getActivity());
        companyHomeRankingAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvCompanyRankingList.setAdapter(companyHomeRankingAdapter);
        initLoadMore();
    }

    private void initLoadMore() {
        companyHomeRankingAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        companyHomeRankingAdapter.getLoadMoreModule().setAutoLoadMore(true);
        companyHomeRankingAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    private void initData() {
        srl.setRefreshing(true);
        String string = "?limit=20&page=1&type=%s&companyId=%s";
        new CompanyAPI().getCompanyTimesRanking(String.format(string, point, companyId), new BaseResultCallback<CompanyHomeTimesRankingEntity>() {
            @Override
            public void onResponse(CompanyHomeTimesRankingEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null) {
                        companyHomeRankingAdapter.setNewData(response.data);
                        if (companyHomeRankingAdapter.getData().size() >= response.count) {
                            companyHomeRankingAdapter.getLoadMoreModule().loadMoreEnd(true);
                            companyHomeRankingAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }
                    }
                }
            }
        });

    }

    private void loadMoreRequested() {
        String string = "?limit=20&page=%s&type=%s&companyId=%s";
        new CompanyAPI().getCompanyTimesRanking(String.format(string, page, point, companyId), new BaseResultCallback<CompanyHomeTimesRankingEntity>() {
            @Override
            public void onResponse(CompanyHomeTimesRankingEntity response) {
                if (response.code == 0) {
                    if (response.data != null) {
                        if (response.data.size() > 0) {
                            companyHomeRankingAdapter.addData(response.data);
                            if (companyHomeRankingAdapter.getData().size() >= response.count) {
                                companyHomeRankingAdapter.getLoadMoreModule().loadMoreEnd(true);
                                companyHomeRankingAdapter.getLoadMoreModule().setEnableLoadMore(false);
                            } else {
                                page++;
                                companyHomeRankingAdapter.getLoadMoreModule().loadMoreComplete();
                                companyHomeRankingAdapter.getLoadMoreModule().setEnableLoadMore(true);
                            }

                        }
                    }
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }
}
